#ifndef _RPC_SESSION_BASE_H_
#define _RPC_SESSION_BASE_H_

#include "IoSocketEx.h"
#include "Header.h"

class CRpcSessionBase : public SGLib::CIoSocketEx
{
public:
    enum
    {
        E_Session_Alive_Timer = 10000,
        E_Session_Reconnect_Timer = 500,
    };

    CRpcSessionBase(s32 id);
    virtual ~CRpcSessionBase();
    
    virtual void OnClose();
    virtual void OnConnect(UserData &ud);
    virtual void OnConnectError(UserData &ud);
    virtual void OnRecv(const byte *pkg, s32 len);
    virtual SGLib::CIoSocketEx* Clone(s32 id);

    u64 GetSessionId();
    u64 GetServerId();
	bool SendMsg(u32 msgid, const IMessage &msg);
	bool SendMsgWithHead(u32 msgid, const IMessage &msg, char *head, int len);
    void KeepAlive();
    bool IsAlive();
    void StartKeepAlive() { _StartKeepAlive(); }
    
    static s32 m_aliveInterval;
    static void SetAliveInterval(s32 interval){ m_aliveInterval = interval; }

protected:
	// 继承这个接口
    virtual void _Clean();
	virtual void _OnMessage(u32 msgid, const byte *msg, s32 len){}

private:
    void _SetSessionId(u64 id);
    static void _ReconnectTimerCallback(void *data, s32 len);
    static void _KeepAliveTimerCallback(void *data, s32 len);
    void _RegisterOnServer();
    void _StartKeepAlive();
    void _StartReconnect(u64 sid);

protected:
    u64 m_sessionId;
    u64 m_serverId; // 主动连接的服务器id
    s32 m_aliveTimer;
    s64 m_lastBeat;
};


#endif

